package com.wuyan.locationdog.ui.my


import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.amap.api.location.AMapLocationListener
import com.amap.api.maps.AMap.OnMarkerClickListener
import com.amap.api.maps.AMapUtils
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.Marker
import com.amap.api.maps.model.MarkerOptions
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bean.PointBean
import com.arcsoft.commonbase.ext.gone
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.BarUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.databinding.MarkMapActivityBinding
import com.wuyan.locationdog.help.DeviceHelp
import com.wuyan.locationdog.help.DogHelp
import com.wuyan.locationdog.ui.device.TrackDetailActivity
import com.wuyan.locationdog.util.maputil.MapHelp
import com.wuyan.locationdog.util.maputil.MapLifecycle
import com.wuyan.locationdog.view.MapPopup
import com.wuyan.locationdog.viewmodel.DeviceViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


class MarkDetailActivity : BaseBindingActivity<MarkMapActivityBinding>() {
    private var marker: Marker? = null
    private lateinit var markerView: View
    private var desc: TextView? = null
    private lateinit var mPointBean: PointBean
    private val mDeviceViewModel: DeviceViewModel by viewModels()
    var isLocation: Boolean = true

    //声明定位回调监听器
    private var mLocationListener = AMapLocationListener { amapLocation ->
        if (amapLocation != null) {
            if (amapLocation.errorCode == 0) {
                //实时位置上报
                Log.e("map", "定位成功:$amapLocation")
                val latLng = LatLng(amapLocation.latitude, amapLocation.longitude)
                binding?.mapView?.map?.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 11F))

                val markerOptions = MapHelp.getMarkerPointUi(layoutInflater, mPointBean, true, latLng)
                marker?.remove()
                marker = binding.mapView.map.addMarker(markerOptions)


            } else {
                //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                Log.e(
                    "map", "location Error, ErrCode:" + amapLocation.errorCode + ", errInfo:" + amapLocation.errorInfo
                )
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        lifecycle.addObserver(MapLifecycle(binding.mapView, savedInstanceState))

    }


    override fun initData() {
        mPointBean = intent.getSerializableExtra("bean") as PointBean

        BarUtils.transparentStatusBar(this)
        binding.apply {
            recyclerView.gone()
            // 设置地图 Marker 点击事件监听器
            mapView.map.setOnMarkerClickListener {
                MapPopup(mActivity).apply {
                    showPopupWindow()
                    mapClick(mPointBean.pointLat, mPointBean.pointLng)
                }
                // 在这里可以实现点击 Marker 时的逻辑
                true
            }
            btnBack.setOnClickListener {
                finish()
            }
            btnLocation.setOnClickListener {
                MapHelp.initLocation(mActivity, mLocationListener)


                isLocation = !isLocation
            }


        }
        MapHelp.initLocation(mActivity, mLocationListener)

    }

    override fun loadNetData() {
        super.loadNetData()

    }

    override fun observe() {
        super.observe()



        mDeviceViewModel.trackBean.observe(this) {
            DeviceHelp.instantTrackBeanContent = it.content
            ActivityUtils.startActivity(TrackDetailActivity::class.java)

        }

    }


}